const { getInsures, addInsures, deleteInsuresById, GrabbleInsuresById,getInsuresId,updateInsures } = require('../dao/insuresDao');
const { moveFiles, deleteFiles } = require('../utils/handleFiles');

// 获取数据
module.exports.getInsures = async (params) => {
 
    if (params.inputs) {
        const data = await GrabbleInsuresById(params);
        return {
            message: '数据搜索成功',
            code: 1,
            data
        }
    } else {
        const data = await getInsures(params);
        return {
            message: '数据获取成功',
            code: 1,
            data
        }
    }

}


// 添加数据
module.exports.addInsures = async product => {
    const data = await addInsures(product);
    console.log();
    if (data) {
        if (data.imageUrl) {
            moveFiles({
                // 旧路径
                fromPath: './public/temp',
                // 新路径
                toPath: './public/images',
                filename: data.imageUrl
            })
            deleteFiles('./public/temp');
        }

        return {

            message: '新增成功',
            code: 1,
            data

        }
    }
    return {
        message: '新增失败',
        code: 0,
    }
}



// 删除数据
module.exports.deleteInsuresById = async id => {
    const data = await deleteInsuresById(id);
  
    if (data.deletedCount > 0) {
        return {
            message: "删除成功",
            code: 1
        }
    }
    return {
        message: "删除失败",
        code: 0
    }
}







// 修改功能获取数据
module.exports.getInsuresId = async (id) => {
    const data = await getInsuresId(id);
    return {
        message: '修改数据获取成功',
        code: 1,
        data
    }

}


// 进行修改
module.exports.updateInsures = async (Insures) => {
    const data = await updateInsures(Insures);
     if(data.nModified>0){
        if (Insures.imageUrl) {
            moveFiles({
                // 旧路径
                fromPath: './public/temp',
                // 新路径
                toPath: './public/images',
                filename: Insures.imageUrl
            })
            deleteFiles('./public/temp');
        }
     
        return {
            message: '修改数据成功',
            code: 1,
            data
        } 
     }
     return {
        message: '修改数据失败',
        code: 0
       
     }   
     
    

}